//
//  QDDoctorAttentionController.m
//  simulateQuickDoctor
//
//  Created by 姚华杰 on 2017/3/23.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDDoctorAttentionController.h"
#import "QDAttentionCell.h"
#import "QDDoctorAttentionMOdel.h"
#import <YYModel.h>
#import "QDNetworkTool.h"
#import "QDDoctorDetailsController.h"
#import "QDDateDoctorController.h"
#import "QDDAModel.h"
#import <MJRefresh.h>
@interface QDDoctorAttentionController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *taBleView;

@property (strong, nonatomic) NSArray<QDDAModel *> *dataList;

@property(nonatomic,strong)MJRefreshNormalHeader *headRefesh;

@property(nonatomic,strong)MJRefreshAutoNormalFooter *footRefesh;
@end

@implementation QDDoctorAttentionController

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册cell
    [_taBleView registerNib:[UINib nibWithNibName:@"QDAttentionCell" bundle:nil] forCellReuseIdentifier:@"Attention"];
    
    //设置数据源
    _taBleView.dataSource = self;
    _taBleView.delegate = self;
    
    //设置预估行高
    _taBleView.estimatedRowHeight = 200;
    //设置真实行高
    _taBleView.rowHeight = UITableViewAutomaticDimension;
    
  
    
    self.navigationItem.title = @"关注医生";
   
    
    [self loadData];
    
    [self setupRefresh];
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QDAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Attention" forIndexPath:indexPath];
    
    QDDAModel *model = _dataList[indexPath.row];
    
    cell.model = model;
    
    return cell;
}


-(void)loadData {
    
    NSDictionary *parameters = @{@"user_id":@1000089,@"page_size":@15,@"page":@1};
    
    NSString* urlString = @"http://iosapi.itcast.cn/doctorList.json.php";
    
    
    QDNetworkTool* tool = [QDNetworkTool sharedTool];
    
    
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    [tool requestWithHTTPMethod:@"POST" URLString:urlString parameters:parameters CallBack:^(id responseObj) {
        
        if(responseObj != nil){
            
            NSString* result = responseObj[@"msg"]; //FAIL
            
            if([result isEqualToString:@"OK"]){
                
                NSDictionary* dict = responseObj[@"data"];
                
                _dataList = [NSArray yy_modelArrayWithClass:[QDDAModel class] json:dict];
                
                [self.taBleView reloadData];
                
                NSLog(@"%ld",_dataList.count);
                [_headRefesh endRefreshing];
                [_footRefesh endRefreshing];
                
                
            }else{
                NSLog(@"请求参数错误");
            }
            
        }else{
            NSLog(@"网络请求错误");
        }
    }];

//    
//    AFHTTPSessionManager *manager = [QDNetworkTool sharedTool];
//    
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html", nil];
//    
//    [manager POST:@"http://iosapi.itcast.cn/doctorList.json.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        NSArray *arrData = responseObject[@"data"];
//        
//        _dataList = [NSArray yy_modelArrayWithClass:[QDDAModel class] json:arrData];
//        
//        [self.taBleView reloadData];
//        
//        NSLog(@"%ld",_dataList.count);
//        [_headRefesh endRefreshing];
//        [_footRefesh endRefreshing];
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        NSLog(@"error-%@",error);
//    }];
//    
}


/**
 选中某行调用此方法

 @param tableView tableView
 @param indexPath 某一行
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        
//    UIStoryboard *docSB = [UIStoryboard storyboardWithName:@"DoctorDetails" bundle:nil];
//    
//    QDDoctorDetailsController *two = [docSB instantiateViewControllerWithIdentifier:@"DoctorDetails"];
    
    
    QDDateDoctorController *two = [[QDDateDoctorController alloc]init];
    two.parameters = @{@"entrance":@"doctorAttention"};
    two.model = _dataList[indexPath.row];
    
    [self.navigationController pushViewController:two animated:YES];
    
    
    
    
    
}

-(void)setupRefresh{
    
    //上拉刷新
    _headRefesh = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    self.taBleView.mj_header = _headRefesh;
    
    //下拉刷新
    _footRefesh = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.taBleView.mj_footer = _footRefesh;
}


@end
